package com.gibs.peixunapi.dao;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.gibs.peixunapi.model.User;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional(rollbackFor = Exception.class)
public interface UserDao extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User> {

	@Query("from User where username = ?1")
	List<User> find(String name);

	@Query("from User where username like ?1")
	List<User> findLike(String name);

	@Query(value = "select * from User limit ?, ?", nativeQuery = true)
	List<User> findPage(Integer offset, Integer size);

	@Query(value = "select * from User where username = ? limit ?, ?", nativeQuery = true)
	List<User> findPage(String name, Integer offset, Integer size);

	@Query(value = "select * from User where username like ? limit ?, ?", nativeQuery = true)
	List<User> findPageLike(String name, Integer offset, Integer size);

	@Query(value = "select count(*) from User where username = ?", nativeQuery = true)
	Long count(String name);

	@Query(value = "select count(*) from User where username like ?", nativeQuery = true)
	Long countLike(String name);

	@Query("from User order by id asc")
	List<User> findAllAsc();

	Page<User> findByIdInAndStatus(List<Integer> idList, Integer status, Pageable pageable);

	Page<User> findByStatus(Integer status, Pageable pageable);

	Page<User> findUsersByUsernameContaining(String name, Pageable pageable1);

	@Modifying(clearAutomatically = true)
	@Query("update User set audit = 1, updateTime =?2 where status = 1 and id in ?1")
	Integer auditBatchUserByIdList(List<Integer> idList, Date date);



	@Modifying(clearAutomatically = true)
	@Query("update User set password = ?2 , updateTime =?3 where status = 1 and id in ?1")
	Integer changePasswordById(Integer id, String password, Date date);

}

